package com.net.oil.TechnologySupport;


import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.net.oil.R;
import com.net.oil.TechnologySupport.adapter.TechnologyListAdapter;
import com.net.oil.TechnologySupport.bean.TechnologyListBean;
import com.net.oil.TechnologySupport.contract.TechnologyListContract;
import com.net.oil.TechnologySupport.presenter.TechnologyListPresenter;
import com.net.oil.app.Constant;
import com.net.oil.base.BaseFragment;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class TechnologyListFragment extends BaseFragment<TechnologyListPresenter> implements TechnologyListContract.View {


    @BindView(R.id.technology_recycler_view)
    RecyclerView technologyRecyclerView;
    Unbinder unbinder;

    private int categoryId = 0;     //分类id
    private int page = 1;           //当前页
    private int rows = 20;          //分页大小
    private boolean haveMore = true;

    private TechnologyListAdapter adapter;

    public TechnologyListFragment() {
    }

    public static TechnologyListFragment newInstance(int id) {
        TechnologyListFragment fragment = new TechnologyListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(Constant.TECHNOLOGY_TYPE_ID, id);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_technology_list, container, false);
        unbinder = ButterKnife.bind(this, view);
        presenter = new TechnologyListPresenter(this);
        Bundle bundle = getArguments();
        if (bundle != null){
            categoryId = bundle.getInt(Constant.TECHNOLOGY_TYPE_ID);
        }
        initView();
        initData();
        return view;
    }
    private void initView() {
        adapter = new TechnologyListAdapter(getContext());
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        technologyRecyclerView.setLayoutManager(manager);
//        technologyRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(), manager.getOrientation()));
        technologyRecyclerView.setAdapter(adapter);

        technologyRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //如果不能向上滚动，有更多则加载更多
                if (!technologyRecyclerView.canScrollVertically(1) && haveMore && page > 1){
                    adapter.changeMoreStatus(Constant.LOADING_MORE);
                    presenter.getTechnologyList(categoryId, page, rows);
                }
            }
        });
    }

    private void initData() {
        presenter.getTechnologyList(categoryId, page, rows);
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    /**
     * 获取技术支持列表成功
     * @param technologyListBean    数据
     */
    @Override
    public void getTechnologyListSuccess(TechnologyListBean technologyListBean) {
        if (technologyListBean != null){
            if (technologyListBean.getList() != null && technologyListBean.getList().size() > 0){
                if (page == 1){
                    adapter.updateData(technologyListBean.getList());
                }else if (page > 1){
                    adapter.loadMore(technologyListBean.getList());
                }
                adapter.changeMoreStatus(Constant.PULL_LOAD_MORE);
                haveMore = true;
                page++;

            }else {
                adapter.changeMoreStatus(Constant.LOADING_NONE);
                haveMore = false;
            }

            /*if (technologyListBean.getPaging() != null){
                if (technologyListBean.getPaging().getTotal() == adapter.getItemCount() + 1){
                    adapter.changeMoreStatus(Constant.LOADING_NONE);
                    haveMore = false;
                }else {
                    adapter.changeMoreStatus(Constant.PULL_LOAD_MORE);
                    haveMore = true;
                    page++;
                }
            }*/
        }
    }

    /**
     * 获取技术支持列表失败
     */
    @Override
    public void getTechnologyListFailed() {

    }
}
